package org.maxgroundstation.simulation.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;

import javax.swing.JPanel;

import org.maxgroundstation.simulation.MaxRobot;


/**
 * A panel for displaying motor power setting.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class MotorPowerPanel extends JPanel {

    // Data members
    private MaxRobot robot;
    
    /**
     * Constructor
     * @param robot the simulated MAX robot.
     */
    public MotorPowerPanel(MaxRobot robot) {
        // Use JPanel constructor.
        super();
        
        this.robot = robot;
        
        setPreferredSize(new Dimension(200, 200));
        
        setBackground(Color.BLACK);
        setOpaque(true);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // Draw motor power.
        drawMotorPower(g2d);
    }
    
    /**
     * Draw the motor power display.
     * @param g2d the graphics context.
     */
    private void drawMotorPower(Graphics2D g2d) {
        
        g2d.setColor(Color.WHITE);
        
        Polygon topTriangle = new Polygon(new int[] {100, 175, 100}, new int[] {25, 25, 100}, 3);
        g2d.draw(topTriangle);
        
        Polygon bottomTriangle = new Polygon(new int[] {100, 25, 100}, new int[] {175, 175, 100}, 3);
        g2d.draw(bottomTriangle);
        
        int fillNum = (int) Math.round(robot.getFrontAxleMotor() * 75D);
        int fillX = 100 + fillNum;
        int fillY = 100 - fillNum;
        Polygon fillTriangle = new Polygon(new int[] {100, fillX, 100}, new int[] { fillY, fillY, 100}, 3);
        g2d.fill(fillTriangle);
    }
}